﻿#include <QVBoxLayout>
#include "TrChartTypes.h"
#include "TrChartKGrids.h"


TrChartKGrids::TrChartKGrids(QWidget *parent) : QWidget(parent)
{
    m_iPeriod = TrKlinePeriod::KP_DAY;

    initLayouts();
    m_pQuoteData = new TrQuoteData;
    connect(m_pQuoteData, SIGNAL(OnReplay(QJsonArray)), this, SLOT(OnQuote(QJsonArray)));
}

TrChartKGrids::~TrChartKGrids()
{
    if(m_pQuoteData != nullptr)
    {
        m_pQuoteData->deleteLater();
        delete m_pQuoteData;
        m_pQuoteData = nullptr;
    }
    if(m_pChartTopBar != nullptr)
    {
        m_pChartTopBar->deleteLater();
        delete m_pChartTopBar;
        m_pChartTopBar = nullptr;
    }
    if(m_pChartMainArea != nullptr)
    {
        m_pChartMainArea->deleteLater();
        delete m_pChartMainArea;
        m_pChartMainArea = nullptr;
    }

}

int TrChartKGrids::getMaxKlineCount(int pos)
{
    if(m_pChartMainArea != nullptr)
    {
        return m_pChartMainArea->width() / pos;
    }
    return 0;
}



void TrChartKGrids::initLayouts()
{
    setMinimumSize(380,200);
    setLayout(new QVBoxLayout);

    m_pChartTopBar = new TrChartTopBar(this);
    connect(m_pChartTopBar->getBarPtr(), SIGNAL(OnChangePeriod(TrKlinePeriod)), this, SLOT(OnPeriodChange(TrKlinePeriod)));


    layout()->addWidget(m_pChartTopBar);
    layout()->setMargin(0);
    layout()->setSpacing(3);

    m_pChartMainArea = new TrChartMainArea;

    layout()->addWidget(m_pChartMainArea);


}

void TrChartKGrids::setCode(const QString& code, const QString& name, double priceTick, int scale)
{
    m_stCode = code;
    m_stName = name;
    m_dPriceTick = priceTick;
    m_iVolScale = scale;
}

void TrChartKGrids::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event)
    this->setAutoFillBackground(true);
    QPalette palette;
    palette.setColor(QPalette::Window, TrColor_BackGrid);
    this->setPalette(palette);
}

void TrChartKGrids::clearData()
{
    m_vdOpen.clear();
    m_vdHigh.clear();
    m_vdLow.clear();
    m_vdClose.clear();
    m_viVolume.clear();
    m_viDatetime.clear();
    m_vKlineDatas.clear();
    m_dmaxPrice = 0;
    m_dminPrice = 0;
}

void TrChartKGrids::OnQuote(QJsonArray json)
{
    clearData();

    for (int i = 0; i < json.size(); i++) {
        QJsonObject obj = json.at(i).toObject();

        QDateTime dt = QDateTime::fromString(obj.value("d").toString(), "yyyy-MM-dd HH:mm:ss");
        double close = QString(obj.value("o").toString()).toDouble();
        double open = QString(obj.value("o").toString()).toDouble();
        double high = QString(obj.value("h").toString()).toDouble();
        double low = QString(obj.value("l").toString()).toDouble();
        int    volume = QString(obj.value("c").toString()).toInt();

        m_viDatetime.push_back(dt);
        m_vdOpen.push_back(open);
        m_vdHigh.push_back(high);
        m_vdLow.push_back(low);
        m_vdClose.push_back(close);
        m_viVolume.push_back(volume);

        WTSBarStruct tBar;
        tBar.close = close;
        tBar.open = open;
        tBar.low = low;
        tBar.high = high;
        tBar.vol = volume;
        tBar.time = dt.toTime_t();
        m_vKlineDatas.push_back(tBar);

    }

    m_dmaxPrice = *max_element(m_vdHigh.begin(),m_vdHigh.end());
    m_dminPrice = *min_element(m_vdLow.begin(),m_vdLow.end());

    qDebug() << m_vKlineDatas.at(0).close;
}

void TrChartKGrids::OnPeriodChange(TrKlinePeriod period)
{
    int iperType = 0;
    switch (period) {
    case TrKlinePeriod::KP_DAY:
        iperType = 0;
        break;
    case TrKlinePeriod::KP_Minute1:
        iperType = 1;
        break;
    case TrKlinePeriod::KP_Minute5:
        iperType = 5;
        break;
    case TrKlinePeriod::KP_Minute15:
        iperType = 15;
        break;
    case TrKlinePeriod::KP_Minute60:
        iperType = 60;
        break;
    default:
        iperType = 0;
        break;
    }

    m_iPeriod = period;
    m_pQuoteData->quote(m_stCode, iperType);
}
